<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>typeahead.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_tree_typeahead.js.html">typeahead.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Provides the typeahead functionality for the tree class.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.ui.tree.TypeAhead&#39;);
<a name="line21"></a>goog.provide(&#39;goog.ui.tree.TypeAhead.Offset&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.array&#39;);
<a name="line24"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line25"></a>goog.require(&#39;goog.string&#39;);
<a name="line26"></a>goog.require(&#39;goog.structs.Trie&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Constructs a TypeAhead object.
<a name="line32"></a> * @constructor
<a name="line33"></a> */
<a name="line34"></a>goog.ui.tree.TypeAhead = function() {
<a name="line35"></a>  this.nodeMap_ = new goog.structs.Trie();
<a name="line36"></a>};
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * Map of tree nodes to allow for quick access by characters in the label text.
<a name="line41"></a> * @type {goog.structs.Trie}
<a name="line42"></a> * @private
<a name="line43"></a> */
<a name="line44"></a>goog.ui.tree.TypeAhead.prototype.nodeMap_;
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * Buffer for storing typeahead characters.
<a name="line49"></a> * @type {string}
<a name="line50"></a> * @private
<a name="line51"></a> */
<a name="line52"></a>goog.ui.tree.TypeAhead.prototype.buffer_ = &#39;&#39;;
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Matching labels from the latest typeahead search.
<a name="line57"></a> * @type {Array.&lt;string&gt;?}
<a name="line58"></a> * @private
<a name="line59"></a> */
<a name="line60"></a>goog.ui.tree.TypeAhead.prototype.matchingLabels_ = null;
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>/**
<a name="line64"></a> * Matching nodes from the latest typeahead search. Used when more than
<a name="line65"></a> * one node is present with the same label text.
<a name="line66"></a> * @type {Array.&lt;goog.ui.tree.BaseNode&gt;?}
<a name="line67"></a> * @private
<a name="line68"></a> */
<a name="line69"></a>goog.ui.tree.TypeAhead.prototype.matchingNodes_ = null;
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Specifies the current index of the label from the latest typeahead search.
<a name="line74"></a> * @type {number}
<a name="line75"></a> * @private
<a name="line76"></a> */
<a name="line77"></a>goog.ui.tree.TypeAhead.prototype.matchingLabelIndex_ = 0;
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>/**
<a name="line81"></a> * Specifies the index into matching nodes when more than one node is found
<a name="line82"></a> * with the same label.
<a name="line83"></a> * @type {number}
<a name="line84"></a> * @private
<a name="line85"></a> */
<a name="line86"></a>goog.ui.tree.TypeAhead.prototype.matchingNodeIndex_ = 0;
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Enum for offset values that are used for ctrl-key navigation among the
<a name="line91"></a> * multiple matches of a given typeahead buffer.
<a name="line92"></a> *
<a name="line93"></a> * @enum {number}
<a name="line94"></a> */
<a name="line95"></a>goog.ui.tree.TypeAhead.Offset = {
<a name="line96"></a>  DOWN: 1,
<a name="line97"></a>  UP: -1
<a name="line98"></a>};
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/**
<a name="line102"></a> * Handles navigation keys.
<a name="line103"></a> * @param {goog.events.BrowserEvent} e The browser event.
<a name="line104"></a> * @return {boolean} The handled value.
<a name="line105"></a> */
<a name="line106"></a>goog.ui.tree.TypeAhead.prototype.handleNavigation = function(e) {
<a name="line107"></a>  var handled = false;
<a name="line108"></a>
<a name="line109"></a>  switch (e.keyCode) {
<a name="line110"></a>    // Handle ctrl+down, ctrl+up to navigate within typeahead results.
<a name="line111"></a>    case goog.events.KeyCodes.DOWN:
<a name="line112"></a>    case goog.events.KeyCodes.UP:
<a name="line113"></a>      if (e.ctrlKey) {
<a name="line114"></a>        this.jumpTo_(e.keyCode == goog.events.KeyCodes.DOWN ?
<a name="line115"></a>                     goog.ui.tree.TypeAhead.Offset.DOWN :
<a name="line116"></a>                     goog.ui.tree.TypeAhead.Offset.UP);
<a name="line117"></a>        handled = true;
<a name="line118"></a>      }
<a name="line119"></a>      break;
<a name="line120"></a>
<a name="line121"></a>    // Remove the last typeahead char.
<a name="line122"></a>    case goog.events.KeyCodes.BACKSPACE:
<a name="line123"></a>      var length = this.buffer_.length - 1;
<a name="line124"></a>      handled = true;
<a name="line125"></a>      if (length &gt; 0) {
<a name="line126"></a>        this.buffer_ = this.buffer_.substring(0, length);
<a name="line127"></a>        this.jumpToLabel_(this.buffer_);
<a name="line128"></a>      } else if (length == 0) {
<a name="line129"></a>        // Clear the last character in typeahead.
<a name="line130"></a>        this.buffer_ = &#39;&#39;;
<a name="line131"></a>      } else {
<a name="line132"></a>        handled = false;
<a name="line133"></a>      }
<a name="line134"></a>      break;
<a name="line135"></a>
<a name="line136"></a>    // Clear typeahead buffer.
<a name="line137"></a>    case goog.events.KeyCodes.ESC:
<a name="line138"></a>      this.buffer_ = &#39;&#39;;
<a name="line139"></a>      handled = true;
<a name="line140"></a>      break;
<a name="line141"></a>  }
<a name="line142"></a>
<a name="line143"></a>  return handled;
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * Handles the character presses.
<a name="line149"></a> * @param {goog.events.BrowserEvent} e The browser event.
<a name="line150"></a> *    Expected event type is goog.events.KeyHandler.EventType.KEY.
<a name="line151"></a> * @return {boolean} The handled value.
<a name="line152"></a> */
<a name="line153"></a>goog.ui.tree.TypeAhead.prototype.handleTypeAheadChar = function(e) {
<a name="line154"></a>  var handled = false;
<a name="line155"></a>
<a name="line156"></a>  if (!e.ctrlKey &amp;&amp; !e.altKey) {
<a name="line157"></a>    // Since goog.structs.Trie.getKeys compares characters during
<a name="line158"></a>    // lookup, we should use charCode instead of keyCode where possible.
<a name="line159"></a>    // Convert to lowercase, typeahead is case insensitive.
<a name="line160"></a>    var ch = String.fromCharCode(e.charCode || e.keyCode).toLowerCase();
<a name="line161"></a>    if (goog.string.isUnicodeChar(ch) &amp;&amp; (ch != &#39; &#39; || this.buffer_)) {
<a name="line162"></a>      this.buffer_ += ch;
<a name="line163"></a>      handled = this.jumpToLabel_(this.buffer_);
<a name="line164"></a>    }
<a name="line165"></a>  }
<a name="line166"></a>
<a name="line167"></a>  return handled;
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * Adds or updates the given node in the nodemap. The label text is used as a
<a name="line173"></a> * key and the node id is used as a value. In the case that the key already
<a name="line174"></a> * exists, such as when more than one node exists with the same label, then this
<a name="line175"></a> * function creates an array to hold the multiple nodes.
<a name="line176"></a> * @param {goog.ui.tree.BaseNode} node Node to be added or updated.
<a name="line177"></a> */
<a name="line178"></a>goog.ui.tree.TypeAhead.prototype.setNodeInMap = function(node) {
<a name="line179"></a>  var labelText = node.getText();
<a name="line180"></a>  if (labelText &amp;&amp; !goog.string.isEmptySafe(labelText)) {
<a name="line181"></a>    // Typeahead is case insensitive, convert to lowercase.
<a name="line182"></a>    labelText = labelText.toLowerCase();
<a name="line183"></a>
<a name="line184"></a>    var previousValue = this.nodeMap_.get(labelText);
<a name="line185"></a>    if (previousValue) {
<a name="line186"></a>      // Found a previously created array, add the given node.
<a name="line187"></a>      previousValue.push(node);
<a name="line188"></a>    } else {
<a name="line189"></a>      // Create a new array and set the array as value.
<a name="line190"></a>      var nodeList = [node];
<a name="line191"></a>      this.nodeMap_.set(labelText, nodeList);
<a name="line192"></a>    }
<a name="line193"></a>  }
<a name="line194"></a>};
<a name="line195"></a>
<a name="line196"></a>
<a name="line197"></a>/**
<a name="line198"></a> * Removes the given node from the nodemap.
<a name="line199"></a> * @param {goog.ui.tree.BaseNode} node Node to be removed.
<a name="line200"></a> */
<a name="line201"></a>goog.ui.tree.TypeAhead.prototype.removeNodeFromMap = function(node) {
<a name="line202"></a>  var labelText = node.getText();
<a name="line203"></a>  if (labelText &amp;&amp; !goog.string.isEmptySafe(labelText)) {
<a name="line204"></a>    labelText = labelText.toLowerCase();
<a name="line205"></a>
<a name="line206"></a>    var nodeList = /** @type {Array} */ (this.nodeMap_.get(labelText));
<a name="line207"></a>    if (nodeList) {
<a name="line208"></a>      // Remove the node from the array.
<a name="line209"></a>      goog.array.remove(nodeList, node);
<a name="line210"></a>      if (!!nodeList.length) {
<a name="line211"></a>        this.nodeMap_.remove(labelText);
<a name="line212"></a>      }
<a name="line213"></a>    }
<a name="line214"></a>  }
<a name="line215"></a>};
<a name="line216"></a>
<a name="line217"></a>
<a name="line218"></a>/**
<a name="line219"></a> * Select the first matching node for the given typeahead.
<a name="line220"></a> * @param {string} typeAhead Typeahead characters to match.
<a name="line221"></a> * @return {boolean} True iff a node is found.
<a name="line222"></a> * @private
<a name="line223"></a> */
<a name="line224"></a>goog.ui.tree.TypeAhead.prototype.jumpToLabel_ = function(typeAhead) {
<a name="line225"></a>  var handled = false;
<a name="line226"></a>  var labels = this.nodeMap_.getKeys(typeAhead);
<a name="line227"></a>
<a name="line228"></a>  // Make sure we have at least one matching label.
<a name="line229"></a>  if (labels &amp;&amp; labels.length) {
<a name="line230"></a>    this.matchingNodeIndex_ = 0;
<a name="line231"></a>    this.matchingLabelIndex_ = 0;
<a name="line232"></a>
<a name="line233"></a>    var nodes = /** @type {Array} */ (this.nodeMap_.get(labels[0]));
<a name="line234"></a>    if ((handled = this.selectMatchingNode_(nodes))) {
<a name="line235"></a>      this.matchingLabels_ = labels;
<a name="line236"></a>    }
<a name="line237"></a>  }
<a name="line238"></a>
<a name="line239"></a>  // TODO(user): beep when no node is found
<a name="line240"></a>  return handled;
<a name="line241"></a>};
<a name="line242"></a>
<a name="line243"></a>
<a name="line244"></a>/**
<a name="line245"></a> * Select the next or previous node based on the offset.
<a name="line246"></a> * @param {goog.ui.tree.TypeAhead.Offset} offset DOWN or UP.
<a name="line247"></a> * @return {boolean} Whether a node is found.
<a name="line248"></a> * @private
<a name="line249"></a> */
<a name="line250"></a>goog.ui.tree.TypeAhead.prototype.jumpTo_ = function(offset) {
<a name="line251"></a>  var handled = false;
<a name="line252"></a>  var labels = this.matchingLabels_;
<a name="line253"></a>
<a name="line254"></a>  if (labels) {
<a name="line255"></a>    var nodes = null;
<a name="line256"></a>    var nodeIndexOutOfRange = false;
<a name="line257"></a>
<a name="line258"></a>    // Navigate within the nodes array.
<a name="line259"></a>    if (this.matchingNodes_) {
<a name="line260"></a>      var newNodeIndex = this.matchingNodeIndex_ + offset;
<a name="line261"></a>      if (newNodeIndex &gt;= 0 &amp;&amp; newNodeIndex &lt; this.matchingNodes_.length) {
<a name="line262"></a>        this.matchingNodeIndex_ = newNodeIndex;
<a name="line263"></a>        nodes = this.matchingNodes_;
<a name="line264"></a>      } else {
<a name="line265"></a>        nodeIndexOutOfRange = true;
<a name="line266"></a>      }
<a name="line267"></a>    }
<a name="line268"></a>
<a name="line269"></a>    // Navigate to the next or previous label.
<a name="line270"></a>    if (!nodes) {
<a name="line271"></a>      var newLabelIndex = this.matchingLabelIndex_ + offset;
<a name="line272"></a>      if (newLabelIndex &gt;= 0 &amp;&amp; newLabelIndex &lt; labels.length) {
<a name="line273"></a>        this.matchingLabelIndex_ = newLabelIndex;
<a name="line274"></a>      }
<a name="line275"></a>
<a name="line276"></a>      if (labels.length &gt; this.matchingLabelIndex_) {
<a name="line277"></a>        nodes = /** @type {Array} */ (this.nodeMap_.get(
<a name="line278"></a>            labels[this.matchingLabelIndex_]));
<a name="line279"></a>      }
<a name="line280"></a>
<a name="line281"></a>      // Handle the case where we are moving beyond the available nodes,
<a name="line282"></a>      // while going UP select the last item of multiple nodes with same label
<a name="line283"></a>      // and while going DOWN select the first item of next set of nodes
<a name="line284"></a>      if (nodes &amp;&amp; nodes.length &amp;&amp; nodeIndexOutOfRange) {
<a name="line285"></a>        this.matchingNodeIndex_ = (offset == goog.ui.tree.TypeAhead.Offset.UP) ?
<a name="line286"></a>                                  nodes.length - 1 : 0;
<a name="line287"></a>      }
<a name="line288"></a>    }
<a name="line289"></a>
<a name="line290"></a>    if ((handled = this.selectMatchingNode_(nodes))) {
<a name="line291"></a>      this.matchingLabels_ = labels;
<a name="line292"></a>    }
<a name="line293"></a>  }
<a name="line294"></a>
<a name="line295"></a>  // TODO(user): beep when no node is found
<a name="line296"></a>  return handled;
<a name="line297"></a>};
<a name="line298"></a>
<a name="line299"></a>
<a name="line300"></a>/**
<a name="line301"></a> * Given a nodes array reveals and selects the node while using node index.
<a name="line302"></a> * @param {Array.&lt;goog.ui.tree.BaseNode&gt;?} nodes Nodes array to select the
<a name="line303"></a> *    node from.
<a name="line304"></a> * @return {boolean} Whether a matching node was found.
<a name="line305"></a> * @private
<a name="line306"></a> */
<a name="line307"></a>goog.ui.tree.TypeAhead.prototype.selectMatchingNode_ = function(nodes) {
<a name="line308"></a>  var node;
<a name="line309"></a>
<a name="line310"></a>  if (nodes) {
<a name="line311"></a>    // Find the matching node.
<a name="line312"></a>    if (this.matchingNodeIndex_ &lt; nodes.length) {
<a name="line313"></a>      node = nodes[this.matchingNodeIndex_];
<a name="line314"></a>      this.matchingNodes_ = nodes;
<a name="line315"></a>    }
<a name="line316"></a>
<a name="line317"></a>    if (node) {
<a name="line318"></a>      node.reveal();
<a name="line319"></a>      node.select();
<a name="line320"></a>    }
<a name="line321"></a>  }
<a name="line322"></a>
<a name="line323"></a>  return !!node;
<a name="line324"></a>};
<a name="line325"></a>
<a name="line326"></a>
<a name="line327"></a>/**
<a name="line328"></a> * Clears the typeahead buffer.
<a name="line329"></a> */
<a name="line330"></a>goog.ui.tree.TypeAhead.prototype.clear = function() {
<a name="line331"></a>  this.buffer_ = &#39;&#39;;
<a name="line332"></a>};
</pre>


</body>
</html>
